package com.sun.androidexerciseproject.function.music.ui;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.sun.androidexerciseproject.function.R;
import com.sun.androidexerciseproject.function.logreport.util.LogUtil;
import com.sun.androidexerciseproject.function.music.service.MediaService;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class MusicMainActivity extends AppCompatActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {
    private static final int REQUEST_CODE_PERMISSION = 124;
    /**
     * paly
     */
    private Button mPlay;
    /**
     * pause
     */
    private Button mPause;
    /**
     * precious
     */
    private Button mPrecious;
    /**
     * next
     */
    private Button mNext;
    private SeekBar mSeekbar;
    private TextView mText1;
    private LinearLayout mActivityMain;
    private MediaService.MyBinder myBinder;
    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MediaService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            myBinder = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_main);
        initView();
    }

    private void initView() {
        mPlay = (Button) findViewById(R.id.play);
        mPlay.setOnClickListener(this);
        mPause = (Button) findViewById(R.id.pause);
        mPause.setOnClickListener(this);
        mPrecious = (Button) findViewById(R.id.precious);
        mPrecious.setOnClickListener(this);
        mNext = (Button) findViewById(R.id.next);
        mNext.setOnClickListener(this);
        mSeekbar = (SeekBar) findViewById(R.id.seekbar);
        mText1 = (TextView) findViewById(R.id.text1);
        mActivityMain = (LinearLayout) findViewById(R.id.activity_main);
        requestPermission();
    }

    private void requestPermission() {
        if (EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            bindService();
        } else {
            EasyPermissions.requestPermissions(this, "请开启权限", REQUEST_CODE_PERMISSION, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
    }

    @Override
    public void onClick(View v) {
        if (null != myBinder) {
            switch (v.getId()) {
                default:
                    break;
                case R.id.play:
                    myBinder.play();
                    break;
                case R.id.pause:
                    myBinder.pause();
                    break;
                case R.id.precious:
                    myBinder.precious();
                    break;
                case R.id.next:
                    myBinder.next();
                    break;
            }
        } else {

        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == REQUEST_CODE_PERMISSION) {
            LogUtil.d("onPermissionsGranted ");
            for (String per : perms) {
                LogUtil.d("onPermissionsGranted per " + per);
            }
            bindService();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (requestCode == REQUEST_CODE_PERMISSION) {
            LogUtil.d("onPermissionsDenied ");
            for (String per : perms) {
                LogUtil.d("onPermissionsDenied per " + per);
            }
        }
    }

    private void bindService() {
        Intent intent = new Intent(this, MediaService.class);
        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != myBinder) {
            myBinder.close();
        }
        unbindService(mServiceConnection);
    }
}
